/*
 * File     : NativeTypes.java
 * Created  : 1 May 2011
 *
 * Copyright © 2011 Matthew Wilson (mj. {my-surname} .uk {at} gmail.com)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.dni.type.nativetype;

import java.util.Map;

import com.googlecode.dni.internal.DniInternal;
import com.googlecode.dni.type.Pointer;


/**
 * <p>
 *  Provides some utilities for dealing with native types.
 * </p>
 *
 * @author Matthew Wilson
 */
public final class NativeTypes
{

    /**
     * Size in bytes of a native <code>char</code>.
     * @see NativeChar
     */
    public static final int NATIVE_CHAR_SIZE;

    /**
     * Size in bytes of a native <code>short&nbsp;int</code>.
     * @see NativeShort
     */
    public static final int NATIVE_SHORT_SIZE;

    /**
     * Size in bytes of a native <code>int</code>.
     * @see NativeInt
     */
    public static final int NATIVE_INT_SIZE;

    /**
     * Size in bytes of a native <code>long&nbsp;int</code>.
     * @see NativeLong
     */
    public static final int NATIVE_LONG_SIZE;

    /**
     * Size in bytes of a native <code>long&nbsp;int</code>.
     * @see WCharT
     */
    public static final int W_CHAR_T_SIZE;

    /**
     * Size in bytes of <code>size_t</code>.
     * @see SizeT
     */
    public static final int SIZE_T_SIZE;

    /**
     * Size in bytes of <code>void*</code>.
     * @see Pointer
     */
    public static final int POINTER_SIZE;

    static
    {
        Map< Class< ? >, Integer > sizes = DniInternal.PLATFORM.getNativeSizes();
        NATIVE_CHAR_SIZE = sizes.get( NativeChar.class );
        NATIVE_SHORT_SIZE = sizes.get( NativeShort.class );
        NATIVE_INT_SIZE = sizes.get( NativeInt.class );
        NATIVE_LONG_SIZE = sizes.get( NativeLong.class );
        W_CHAR_T_SIZE = sizes.get( WCharT.class );
        SIZE_T_SIZE = sizes.get( SizeT.class );
        POINTER_SIZE = sizes.get( Pointer.class );

        if ( POINTER_SIZE != 4 && POINTER_SIZE != 8 )
        {
            throw new AssertionError( "Unsupported pointer size: " + POINTER_SIZE );
        }

        for ( Map.Entry< Class< ? >, Integer > entry : sizes.entrySet() )
        {
            int size = entry.getValue();
            if ( size != 1 && size != 2 && size != 4 && size != 8 )
            {
                throw new AssertionError( "Unsupported size for " + entry.getKey() + ":" + size );
            }
        }
    }

    /** Prevents instantiation. */
    private NativeTypes()
    {
        throw new UnsupportedOperationException();
    }

}
